﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ADA.Entity;
using ADA.Data;
using System.Data.SqlClient;

namespace ADA.Business
{
    public class BOLTrading
    {
        public bool viewAllPost(out DataTable tblPost, out string message)
        {
            tblPost = new DataTable();
            message = "";
            DALPost dalPost = new DALPost();
            try
            {
                tblPost = dalPost.getAllPost();

                DataColumn column;
                short status;
                // Them cot Status
                column = new DataColumn();
                column.ColumnName = "StatusName";
                column.DataType = typeof(string);
                column.DefaultValue = "";
                tblPost.Columns.Add(column);

                // Lay status theo statusID
                status = 0;
                for (int i = 0; i < tblPost.Rows.Count; i++)
                {
                    status = Convert.ToInt16(tblPost.Rows[i]["Status"]);
                    tblPost.Rows[i]["StatusName"] = BOLCommon.getPostStatus(status);
                }
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool deletePost(long postID, out string message)
        {
            message = "";
            DALPost dalPost = new DALPost();
            DALOrder dalOrder = new DALOrder();
            try
            {
                dalOrder.deleteOrderByPostID(postID);
                dalPost.deletePostByPostID(postID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }

        public bool viewPostDetail(long postID, out DataTable tblpost, out string message)
        {
            tblpost = new DataTable();
            message = "";
            DALPost dalPost = new DALPost();
            try
            {
                tblpost = dalPost.getPostInfo(postID);
                return true;
            }
            catch (SqlException)
            {
                message = Common.MESSAGE_ERROR_SQL_FAIL;
                return false;
            }
            catch (Exception)
            {
                message = Common.MESSAGE_ERROR_CONNECT_DATABASE_FAIL;
                return false;
            }
        }
    }
}
